package com.ruoyi.standingbook.lamp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.asset.homepage.constant.StatusConstant;
import com.ruoyi.common.utils.BeanCopyUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.standingbook.lamp.domain.bo.LampPicBo;
import com.ruoyi.standingbook.lamp.domain.vo.LampImportDetailVo;
import com.ruoyi.standingbook.lamp.domain.vo.LampPicVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.standingbook.lamp.domain.bo.LampBo;
import com.ruoyi.standingbook.lamp.domain.vo.LampVo;
import com.ruoyi.standingbook.lamp.domain.Lamp;
import com.ruoyi.standingbook.lamp.mapper.LampMapper;
import com.ruoyi.standingbook.lamp.service.ILampService;

import java.util.*;

/**
 * 路灯管养任务完成进度Service业务层处理
 *
 * @author cc
 * @date 2023-08-29
 */
@RequiredArgsConstructor
@Service
public class LampServiceImpl implements ILampService {

    private final LampMapper baseMapper;

    /**
     * 查询路灯管养任务完成进度
     */
    @Override
    public LampVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询路灯管养任务完成进度列表
     */
    @Override
    public TableDataInfo<LampVo> queryPageList(LampBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Lamp> lqw = buildQueryWrapper(bo);
        Page<LampVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询路灯管养任务完成进度列表
     */
    @Override
    public List<LampVo> queryList(LampBo bo) {
        LambdaQueryWrapper<Lamp> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Lamp> buildQueryWrapper(LampBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Lamp> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getItemName()), Lamp::getItemName, bo.getItemName());
        lqw.eq(bo.getServiceType()!=null, Lamp::getServiceType, bo.getServiceType());
        lqw.eq(StringUtils.isNotBlank(bo.getLampType()), Lamp::getLampType, bo.getLampType());
        lqw.eq(StringUtils.isNotBlank(bo.getLampProperties()), Lamp::getLampProperties, bo.getLampProperties());
        lqw.eq(bo.getPolesCount() != null, Lamp::getPolesCount, bo.getPolesCount());
        lqw.eq(bo.getLampCount() != null, Lamp::getLampCount, bo.getLampCount());
        lqw.eq(bo.getBoxCount() != null, Lamp::getBoxCount, bo.getBoxCount());
        lqw.eq(bo.getVoltageCount() != null, Lamp::getVoltageCount, bo.getVoltageCount());
        lqw.eq(bo.getDate() != null, Lamp::getDate, bo.getDate());
        lqw.eq(StringUtils.isNotBlank(bo.getCommunity()), Lamp::getCommunity, bo.getCommunity());
        lqw.eq(StringUtils.isNotBlank(bo.getAddress()), Lamp::getAddress, bo.getAddress());
        lqw.eq(bo.getFrequency() != null, Lamp::getFrequency, bo.getFrequency());
        lqw.eq(bo.getActualFrequency() != null, Lamp::getActualFrequency, bo.getActualFrequency());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), Lamp::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getPic1()), Lamp::getPic1, bo.getPic1());
        lqw.eq(StringUtils.isNotBlank(bo.getPic2()), Lamp::getPic2, bo.getPic2());
        lqw.eq(StringUtils.isNotBlank(bo.getPic3()), Lamp::getPic3, bo.getPic3());
        lqw.eq(StringUtils.isNotBlank(bo.getPic4()), Lamp::getPic4, bo.getPic4());
        lqw.eq(StringUtils.isNotBlank(bo.getProviderId()), Lamp::getProviderId, bo.getProviderId());

        lqw.between(params.get("beginDate") != null && params.get("endDate") != null,
                Lamp::getDate ,params.get("beginDate"), params.get("endDate"));

        return lqw;
    }

    /**
     * 新增路灯管养任务完成进度
     */
    @Override
    public Boolean insertByBo(LampBo bo) {
        Lamp add = BeanUtil.toBean(bo, Lamp.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改路灯管养任务完成进度
     */
    @Override
    public Boolean updateByBo(LampBo bo) {
        Lamp update = BeanUtil.toBean(bo, Lamp.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Lamp entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除路灯管养任务完成进度
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public Boolean batchInsert(List<LampImportDetailVo> voList) {
        if (null != voList && !voList.isEmpty()) {
            Date date = voList.get(0).getDate();
            date.setHours(0);
            date.setMinutes(0);
            date.setSeconds(0);
            LambdaQueryWrapper<Lamp> lqw = Wrappers.lambdaQuery();
            lqw.eq(Lamp::getDate, date);
            this.baseMapper.delete(lqw);
        }

        List<Lamp> list = BeanCopyUtils.copyList(voList, Lamp.class);
        list.forEach(e -> {
            e.setId(null);
            e.restBase();
        });
        return this.baseMapper.insertBatch(list);
    }

    @Override
    public List<LampPicVo> getPicByDate(String date) {
        List<LampPicBo> lampPicBos = baseMapper.selectPicByDate(date);
        List<LampPicVo> pics = new ArrayList<>();
        lampPicBos.forEach(e->{
            if (StringUtils.isNotBlank(e.getPic1())){
                LampPicVo lampPicVo = new LampPicVo();
                lampPicVo.setPic(e.getPic1());
                pics.add(lampPicVo);
            }
            if (StringUtils.isNotBlank(e.getPic2())){
                LampPicVo lampPicVo = new LampPicVo();
                lampPicVo.setPic(e.getPic2());
                pics.add(lampPicVo);
            }
            if (StringUtils.isNotBlank(e.getPic3())){
                LampPicVo lampPicVo = new LampPicVo();
                lampPicVo.setPic(e.getPic3());
                pics.add(lampPicVo);
            }
            if (StringUtils.isNotBlank(e.getPic4())){
                LampPicVo lampPicVo = new LampPicVo();
                lampPicVo.setPic(e.getPic4());
                pics.add(lampPicVo);
            }
        });
        return pics;
    }

    @Override
    public Integer getSumByDate(String date) {
        return baseMapper.selectSumByDate(date,null, StatusConstant.FINISHED);
    }

    @Override
    public Integer getSumByDateAndCommunity(String date, String community) {
        return baseMapper.selectSumByDate(date,community, StatusConstant.FINISHED);
    }

    @Override
    public List<LampVo> queryListByDate(String date, String community) {
        LambdaQueryWrapper<Lamp> lqw = Wrappers.lambdaQuery();
        lqw.eq(Lamp::getDate,date);
        if (StringUtils.isNotEmpty(community)){
            lqw.eq(Lamp::getCommunity,community);
        }
        lqw.orderByDesc(Lamp::getPolesCount);
        return baseMapper.selectVoList(lqw);
    }
}
